package com.czy.owner.utils;

import android.app.Dialog;
import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.czy.owner.R;


/**
 * Created by wangyang on 2017/4/27 0021.
 */

public class ToastDialog extends Dialog {
    public ToastDialog(Context context, int theme) {
        super(context, theme);
    }

    public ToastDialog(Context context) {
        super(context);
    }

    public static class Builder {

        private Context context;
        private String message;
        private View contentView;

        public Builder(Context context) {
            this.context = context;
        }


        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }


        public Builder setMessage(int message) {
            this.message = (String) context.getText(message);
            return this;
        }


        public Builder setContentView(View v) {
            this.contentView = v;
            return this;
        }


        public ToastDialog create() {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final ToastDialog dialog = new ToastDialog(context, R.style.tip_dialog);
            View layout = inflater.inflate(R.layout.tip_dialog, null);
            dialog.addContentView(layout, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
            if (message != null) {
                ((TextView) layout.findViewById(R.id.tv_warmdialog)).setText(message);
            }
            dialog.setContentView(layout);
            return dialog;
        }
    }

    public static void ShowDialogMessage(Context context, String warmInfo) {
        Builder customBuilder = new
                Builder(context);
        customBuilder.setMessage(warmInfo);
        final ToastDialog customDialog = customBuilder.create();
        customDialog.show();
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            public void run() {
                customDialog.dismiss();
            }
        }, 1000);
    }

}